package io.renren.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.dto.ArticlesDTO;
import io.renren.excel.ArticlesExcel;
import io.renren.service.ArticlesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2025-07-15
 */
@RestController
@RequestMapping("demo/articles")
@Tag(name = "文章管理") // 为 Tag 添加了有意义的名称
public class ArticlesController {
    @Autowired
    private ArticlesService articlesService;

    @GetMapping("page")
    @Operation(summary = "分页查询文章列表") // 更具描述性的摘要
    public Result<PageData<ArticlesDTO>> page(
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, schema = @Schema(type = "integer", format = "int32")) Integer page,
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, schema = @Schema(type = "integer", format = "int32")) Integer limit,
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, schema = @Schema(type = "string")) String orderField,
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, schema = @Schema(type = "string", allowableValues = {"asc", "desc"})) String order,
            @Parameter(hidden = true) @RequestParam Map<String, Object> params // 隐藏此参数，因为已单独定义
    ){
        // 将单独的参数放入Map中，以便 service 层可以统一处理
        params.put(Constant.PAGE, page);
        params.put(Constant.LIMIT, limit);
        params.put(Constant.ORDER_FIELD, orderField);
        params.put(Constant.ORDER, order);

        PageData<ArticlesDTO> pageData = articlesService.page(params);

        return new Result<PageData<ArticlesDTO>>().ok(pageData);
    }

    @GetMapping("{id}")
    @Operation(summary = "获取单篇文章信息") // 更具描述性的摘要
    @Parameter(name = "id", description = "文章ID", in = ParameterIn.PATH, required = true, schema = @Schema(type = "integer", format = "int64"))
    @RequiresPermissions("demo:articles:info")
    public Result<ArticlesDTO> get(@PathVariable("id") Long id){
        ArticlesDTO data = articlesService.get(id);

        return new Result<ArticlesDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存文章") // 更具描述性的摘要
    @LogOperation("保存文章")
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "文章数据传输对象", required = true)
    @RequiresPermissions("demo:articles:save")
    public Result save(@RequestBody ArticlesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        articlesService.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改文章") // 更具描述性的摘要
    @LogOperation("修改文章")
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "文章数据传输对象", required = true)
    @RequiresPermissions("demo:articles:update")
    public Result update(@RequestBody ArticlesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        articlesService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除文章") // 更具描述性的摘要
    @LogOperation("删除文章")
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "要删除的文章ID数组", required = true)
    @RequiresPermissions("demo:articles:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        articlesService.delete(ids);

        return new Result();
    }



}